package com.laf.manage.controller.user;

import com.laf.common.core.controller.BaseController;
import com.laf.common.core.domain.R;
import com.laf.common.exception.ServiceException;
import com.laf.manage.domain.po.Category;
import com.laf.manage.service.ICategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 分类管理Controller
 *
 * @author zian
 * @date 2024-10-30
 */
@Api(tags = "分类管理管理")
@RestController("userCategoryController")
@RequestMapping("/user/categorys")
@RequiredArgsConstructor
public class CategoryController extends BaseController {

    private final ICategoryService categoryService;

    /**
     * 获取分类详情
     *
     * @param id 分类id
     * @return 分类po
     */
    @GetMapping("{id}")
    @ApiOperation("获取分类详情")
    public R<Category> getCategoryById(@PathVariable Long id) {
        Category category = categoryService.getById(id);
        if (category == null) throw new ServiceException("分类不存在");
        return R.ok(category);
    }

    /**
     * 获取分类列表
     *
     * @return
     */
    @GetMapping("/tree/list")
    @ApiOperation("获取分类列表")
    public R<List<Category>> getCategoryTreeList() {
        List<Category> list = categoryService.getCategoryTreeList();
        return R.ok(list);
    }
}
